package com.sdx.console.chart.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.BaseEntity;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.console.common.entity.BaseProvince;
import com.sdx.console.common.service.BaseDataService;
import com.sdx.console.utils.BigDecimalUntil;
import org.beetl.sql.core.SQLManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chart/contract")
public class ContractChartController extends AuthorizedBaesController {

    private static final Logger log = LoggerFactory.getLogger(CustomerChartController.class);
    private static final String MODEL = "/chart/contract";
    private static final String SQL_CUS = "chart.customer";
    private static final String SQL = "chart.contract";

    @Resource
    private SQLManager sqlManager;

    @GetMapping("/index.do")
    @Function("chart.contract.query")
    @ResponseBody
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
        return view;
    }

    @PostMapping("/getProv.json")
    @ResponseBody
    public List<BaseProvince> getProv(HttpServletRequest request) {
        return sqlManager.select(SQL_CUS+".getProv",BaseProvince.class);
    }

    @PostMapping("/histogram.json")
    @ResponseBody
    public JsonResult histogram(String provIds,String type,Integer year) {
        Map<String,Object> param = new HashMap<>();
        param.put("provIds",provIds);
        param.put("type",type);
        param.put("year",year);
        List<BaseEntity> lists = sqlManager.select(SQL + ".selectCountProvDrilldown", BaseEntity.class,param);
        String name = "";
        JSONArray array =  new JSONArray();
        List<JSONObject> jsonList = new ArrayList<>();
        JSONObject json = new JSONObject();
        for (BaseEntity entity : lists) {
            String id = entity.get("id").toString();
            if(!name.equals(id)){
                array = new JSONArray();
                json = new JSONObject();
                json.put("name",id+'月');
                json.put("id",id+'月');
                jsonList.add(json);
                json.put("data",array);
                name = id;
            }
            Object[] obj = new Object[2];
            obj[0]=entity.get("provName");
            obj[1]=entity.get("total");
            array.add(obj);
        }
        List<BaseEntity> list = sqlManager.select(SQL + ".selectCountProv", BaseEntity.class,param);
        List<JSONObject> objects = new ArrayList<>();
        for (BaseEntity baseEntity : list) {
            JSONObject object = new JSONObject();
            object.put("name",baseEntity.get("id").toString()+'月');
            object.put("y",baseEntity.get("total"));
            object.put("drilldown",baseEntity.get("id").toString()+'月');
            objects.add(object);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",objects);
        jsonObject.put("series",jsonList);
        return JsonResult.success(jsonObject);
    }


    @PostMapping("/piechart.json")
    @ResponseBody
    public JsonResult piechart(String provIds,String type,Integer year) {
        Map<String,Object> param = new HashMap<>();
        param.put("provIds",provIds);
        param.put("type",type);
        param.put("year",year);
        List<BaseEntity> lists = sqlManager.select(SQL + ".selectPieChart", BaseEntity.class,param);
        Integer count = sqlManager.intValue(SQL + ".selectPieChartCount", param);
        List<JSONObject> list = new ArrayList<>();
        for (BaseEntity baseEntity : lists) {
            JSONObject json = new JSONObject();
            json.put("count",baseEntity.get("total"));
            json.put("name",baseEntity.get("provName"));
            Double rate = 0d;
            if(count>0){
                rate =  BigDecimalUntil.rate(Integer.parseInt(baseEntity.get("total").toString()),count,2);
            }
            json.put("y", rate);
            list.add(json);
        }
        return JsonResult.success(list);
    }


    @PostMapping("/curve.json")
    @ResponseBody
    public JsonResult curve(String provIds,String type,Integer year) {
        Map<String,Object> param = new HashMap<>();
        param.put("provIds",provIds);
        param.put("type",type);
        param.put("year",year);
        param.put("provIds",provIds);
        List<BaseEntity> lists = sqlManager.select(SQL + ".selectCurve", BaseEntity.class,param);
        String name = "";
        List<JSONObject> jsonList = new ArrayList<>();
        JSONObject json = new JSONObject();
        int[] ints = new int[12];
        for (int i = 0; i < lists.size(); i++) {
            BaseEntity entity = lists.get(i);
            String provName = entity.get("provName").toString();
            if(!name.equals(provName)){
                ints = new int[12];
                json = new JSONObject();
                json.put("name",provName);
                jsonList.add(json);
                json.put("data",ints);
                name = provName;
            }
            ints[i%12] = Integer.parseInt(entity.get("total").toString());
        }
        return JsonResult.success(jsonList);
    }


}
